package pl.lodz.p.browarex.contoller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import pl.lodz.p.browarex.model.Ingredient;
import pl.lodz.p.browarex.model.Product;
import pl.lodz.p.browarex.model.ProductOrder;
import pl.lodz.p.browarex.service.GenericService;
import pl.lodz.p.browarex.service.ProductService;
import pl.lodz.p.browarex.util.DateUtil;
import pl.lodz.p.browarex.ws.BeerEndpoint;

@Controller
@RequestMapping("products")
public class ProductController {
	@Inject
	private BeerEndpoint be;

	@Autowired
	private ProductService<Product, Long> productService;
	@Autowired
	private GenericService<ProductOrder, Long> productOrderService;
	
	@Autowired
	private GenericService<Ingredient, Long> ingredientService;
	
	
	@RequestMapping()
	public ModelAndView showLoginPage(Model model) throws Exception {
		ModelAndView mav = new ModelAndView("products");
		List<Product> productList = productService.list();
		productList= changeBundle(productList);
		mav.addObject(productList);

		return mav;
	}

	@RequestMapping(params = "operation=productDetails")
	public ModelAndView productDetail(@RequestParam("selectedId") long id,
			ModelMap model) throws Exception {
		ModelAndView mav = new ModelAndView("productDetails");
		Product product = productService.find(id);
		product.setDescription("");
		List<Ingredient> ingredientList= ingredientService.list();

		mav.addObject(ingredientList);
		
		mav.addObject("product", product);
		return mav;
	}
	
	@RequestMapping(params = "operation=productRemove")
	public ModelAndView productRemove(@RequestParam("selectedId") long id,
			ModelMap model) throws Exception {
		ModelAndView mav = new ModelAndView("products");
		Product product = productService.find(id);
		List<ProductOrder> productOrderList = productOrderService.findByProperty("productId", product.getProductId());
		
		
		for(ProductOrder po: productOrderList){
			productOrderService.delete(po);
		}
		
		productService.delete(product);
		
		List<Product> productList = productService.list();
		productList= changeBundle(productList);
		mav.addObject(productList);
		
		mav.addObject("p", product);
		return mav;
	}
	@RequestMapping(params = "operation=start")
	public ModelAndView start(@RequestParam("selectedId") long id,@RequestParam("amount") int amount,
			ModelMap model) throws Exception {
		ModelAndView mav = new ModelAndView("products");
		Product product = productService.find(id);
		
		DateUtil dateUtil = new DateUtil();

		ProductOrder po = new ProductOrder();
		po.setAmount(amount);
		po.setDate(new Date());
		po.setProduct(product);
		po.setProductId(product.getProductId());
		po.setProductionID(UUID.randomUUID().toString());
		po.setRealization(false);
		productOrderService.upsert(po);

		List<Product> productList = productService.list();
		productList= changeBundle(productList);
		mav.addObject(productList);
		
		mav.addObject("p", product);
		return mav;
	}
	private List<Product> changeBundle(List<Product> productList){
		
		for(Product p: productList){
			if(p.getBundle().equals("BOTTLE")){
				p.setBundle("Butelka");
			}else if(p.getBundle().equals("CAN")){
				p.setBundle("Puszka");
			}else if(p.getBundle().equals("PLASTIC")){
				p.setBundle("Butelka plastikowa");
			}else if(p.getBundle().equals("KEG")){
				p.setBundle("Keg");
			}else if(p.getBundle().equals("BARREL")){
				p.setBundle("Beczka");
			}
		}
		
		return productList;
	}
}
